package com.arcaneshift.model.action.game;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Transient;

import com.arcaneshift.model.abstracts.AbstractCard;

@Entity
public class Deck implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
	
	@Basic
	private String name;
	
	@Basic
	private Long playerId;
	
	@Transient
	private List<AbstractCard> cards;

	/**
	 * Shuffle the cards in the deck
	 */
	public void shuffle() {
		List<AbstractCard> shuffeledCards = new ArrayList<AbstractCard>();
		Random random = new Random();
		int count = cards.size();
		
		for (int i = 0; i < count; i++) {
			shuffeledCards.add(cards.remove(random.nextInt(cards.size())));
		}
		cards = shuffeledCards;
	}
	
	/**
	 * Get the card from the top of the deck if there is one.
	 * 
	 * @return
	 */
	public AbstractCard drawTopCard() {
		if (cards.size() > 0) {
			return cards.remove(cards.size() - 1);
		}
		return null;
	}
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<AbstractCard> getCards() {
		return cards;
	}

	public void setCards(List<AbstractCard> cards) {
		this.cards = cards;
	}
	
	public Long getPlayerId() {
		return playerId;
	}

	public void setPlayerId(Long playerId) {
		this.playerId = playerId;
	}

	@Transient
	public static final String ID = "id";
	@Transient
	public static final String NAME = "name";
	@Transient
	public static final String PLAYER_ID = "playerId";
}
